<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>后台管理-登陆</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.5.5/css/layui.css}" media="all">
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        body {

            height: 100%;
            width: 100%;
        }

        #container {
            height: 100%;
            width: 100%;
        }

        input:-webkit-autofill {
            -webkit-box-shadow: inset 0 0 0 1000px #fff;
            background-color: transparent;
        }

        .admin-login-background {
            width: 300px;
            height: 300px;
            position: absolute;
            left: 50%;
            top: 40%;
            margin-left: -150px;
            margin-top: -100px;
        }

        .admin-header {
            text-align: center;
            margin-bottom: 20px;
            color: #ffffff;
            font-weight: bold;
            font-size: 40px
        }

        .admin-input {
            border-top-style: none;
            border-right-style: solid;
            border-bottom-style: solid;
            border-left-style: solid;
            height: 50px;
            width: 300px;
            padding-bottom: 0px;
        }

        .admin-input::-webkit-input-placeholder {
            color: #a78369
        }

        .layui-icon-username {
            color: #a78369 !important;
        }

        .layui-icon-username:hover {
            color: #9dadce !important;
        }

        .layui-icon-password {
            color: #a78369 !important;
        }

        .layui-icon-password:hover {
            color: #9dadce !important;
        }

        .admin-input-username {
            border-top-style: solid;
            border-radius: 10px 10px 0 0;
        }

        .admin-input-verify {
            border-radius: 0 0 10px 10px;
        }

        .admin-button {
            margin-top: 20px;
            font-weight: bold;
            font-size: 18px;
            width: 300px;
            height: 50px;
            border-radius: 5px;
            background-color: #a78369;
            border: 1px solid #d8b29f
        }

        .admin-icon {
            margin-left: 260px;
            margin-top: 10px;
            font-size: 30px;
        }

        i {
            position: absolute;
        }

        .admin-captcha {
            position: absolute;
            margin-left: 205px;
            margin-top: -40px;
        }

        .canvas {
            position: absolute;
            margin-top: -45px;
            margin-left: 170px;
            width: 300px;
            height: 140px;
        }
    </style>
</head>
<body th:style="'background-image: url(/img/bg.jpg)'">
<div id="container">
    <div></div>
    <div class="admin-login-background">
        <div class="admin-header">
            <span>layuimini</span>
        </div>
        <form class="layui-form" action="">
            <div>
                <i class="layui-icon layui-icon-username admin-icon"></i>
                <input type="text" name="username" lay-verify="username" placeholder="请输入用户名" autocomplete="off"
                       class="layui-input admin-input admin-input-username" value="" id="username">
            </div>
            <div>
                <i class="layui-icon layui-icon-password admin-icon"></i>
                <input type="password" name="password" lay-verify="password" placeholder="请输入密码" autocomplete="off"
                       class="layui-input admin-input" value="" id="password">
            </div>
            <div>
                <input type="text" name="captcha" lay-verify="captcha" placeholder="请输入验证码" autocomplete="off"
                       class="layui-input admin-input admin-input-verify" value=""
                       style="" id="captcha">
                <canvas id="canvas" class="canvas"></canvas>
            </div>
            <button class="layui-btn admin-button" lay-submit="" lay-filter="login" id="login">登 陆</button>
        </form>
    </div>
</div>
</body>
<script th:src="@{/layuimini/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script>
    var canvas = document.getElementById("canvas");//演员
    var context = canvas.getContext("2d");//舞台，getContext() 方法可返回一个对象，该对象提供了用于在画布上绘图的方法和属性。
    var button = document.getElementById("login");//获取按钮
    draw();
    canvas.onclick = function () {
        context.clearRect(0, 0, 120, 40);//在给定的矩形内清除指定的像素
        draw();
    }

    // 随机颜色函数
    function getColor() {
        var r = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);
        return "rgb(" + r + "," + g + "," + b + ")";
    }

    var num //定义容器接收验证码
    function draw() {
        context.strokeRect(0, 0, 120, 40);//绘制矩形（无填充）
        var aCode = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h"];
        // 绘制字母
        var arr = [] //定义一个数组用来接收产生的随机数

        for (var i = 0; i < 4; i++) {
            var x = 20 + i * 20;//每个字母之间间隔20
            var y = 20 + 10 * Math.random();//y轴方向位置为20-30随机
            var index = Math.floor(Math.random() * aCode.length);//随机索引值
            var txt = aCode[index];
            context.font = "bold 20px 微软雅黑";//设置或返回文本内容的当前字体属性
            context.fillStyle = getColor();//设置或返回用于填充绘画的颜色、渐变或模式，随机
            context.translate(x, y);//重新映射画布上的 (0,0) 位置，字母不可以旋转移动，所以移动容器
            var deg = 90 * Math.random() * Math.PI / 180;//0-90度随机旋转
            context.rotate(deg);// 	旋转当前绘图
            context.fillText(txt, 0, 0);//在画布上绘制“被填充的”文本
            context.rotate(-deg);//将画布旋转回初始状态
            context.translate(-x, -y);//将画布移动回初始状态
            arr[i] = txt //接收产生的随机数
        }
        num = arr[0] + arr[1] + arr[2] + arr[3] //将产生的验证码放入num
        // 绘制干扰线条
        for (var i = 0; i < 8; i++) {
            context.beginPath();//起始一条路径，或重置当前路径
            context.moveTo(Math.random() * 120, Math.random() * 40);//把路径移动到画布中的随机点，不创建线条
            context.lineTo(Math.random() * 120, Math.random() * 40);//添加一个新点，然后在画布中创建从该点到最后指定点的线条
            context.strokeStyle = getColor();//随机线条颜色
            context.stroke();// 	绘制已定义的路径
        }
        // 绘制干扰点，和上述步骤一样，此处用长度为1的线代替点
        for (var i = 0; i < 20; i++) {
            context.beginPath();
            var x = Math.random() * 120;
            var y = Math.random() * 40;
            context.moveTo(x, y);
            context.lineTo(x + 1, y + 1);
            context.strokeStyle = getColor();
            context.stroke();
        }
    }

</script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            jquery = layui.jquery;
        $ = layui.$;

        // 登录过期的时候，跳出ifram框架
        if (top.location != self.location) top.location = self.location;

        //自定义验证规则
        form.verify({
            username: function (value) {
                if (value.length < 1) {
                    return '用户名不能为空'
                }
            },
            password: function (value) {
                if (value.length < 6) {
                    return '密码不得少于6位'
                }
            },
            captcha: function (value) {
                if (value.length < 1) {
                    return '验证码不能为空'
                }
                if (value !== num) {
                    return '验证码错误'
                }
            },
            content: function (value) {
                layedit.sync(editIndex)
            }
        });
        // 进行登录操作
        form.on('submit(login)', function (data) {

            jquery.ajax({
                method: 'post',
                dataType: 'json',
                url: '/shop/login',
                data: {username: data.field.username, pwd: data.field.password},
                success: function (message) {
                    if (message.code === "0") {
                        layer.msg('登录成功', function () {
                            window.location = './index?userId=' + message.usr_ID;
                        });
                    } else {
                        layer.alert(message.msg, {
                            title: '回执'
                        })
                        $('#username').val("")
                        $('#password').val("")
                        $('#captcha').val("")
                    }
                }
            })
            return false;
        });
    });
</script>
</html>